package work.huangxin.share.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import work.huangxin.share.domain.admin.UserBlock;
import work.huangxin.share.domain.admin.dto.UserCountDto;
import work.huangxin.share.domain.other.UserMessage;
import work.huangxin.share.mapper.AdminUserMapper;
import work.huangxin.share.mapper.UserMessageMapper;
import work.huangxin.share.service.AdminUserService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 钟声浩
* @date 2023/4/4 18:39
*/
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Resource
    private AdminUserMapper adminUserMapper;

    @Resource
    private UserMessageMapper userMessageMapper;

    //分页查询所有用户
    @Override
    public PageInfo<UserMessage> selectPageUserList(UserMessage userMessage, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserMessage> userMessageList = adminUserMapper.selectPageUserList(userMessage);
        return PageInfo.of(userMessageList);
    }


    //根据userId查询用户详细信息
    @Override
    public UserMessage getUserDetailByUserId(Integer userId) {
        return userMessageMapper.getUserMessage(userId);
    }


    @Override
    public int getNewUserCount() {
        //获得当前时间
        DateTime nowDate = DateTime.now();
        //获得当月的第一天
        String firstDayTime = nowDate.setField(DateField.DAY_OF_MONTH, 1).toString();
        String lastDayTime = nowDate.setField(DateField.MONTH, nowDate.getField(DateField.MONTH) + 1).toString();
        return userMessageMapper.getNewUserCount(firstDayTime,lastDayTime);
    }

    //拉黑用户，存进数据库中
    @Override
    public int addBlockUser(UserBlock userBlock) {
        return adminUserMapper.addBlockUser(userBlock);
    }

    //解除对用户的拉黑
    @Override
    public int unlockUser(Integer userId) {
        return adminUserMapper.unlockUser(userId);
    }

    //得到所有拉黑的用户
    @Override
    public List<UserBlock> selectAllBlock() {
        return adminUserMapper.selectAllBlock();
    }

    //进行事务操作
    @Transactional
    public void unlockUserAndAllow(Integer userId) {
        long retryInterval = 1000;//时间间隔
        int retryTimes = 0;//当前尝试次数
        int maxRetryTimes = 3;//最大尝试次数
        boolean success = false;
        while (!success && retryTimes < maxRetryTimes) {
            try {
                adminUserMapper.unlockUser(userId);
                userMessageMapper.updateUserAllowByUserId(userId);
                success = true;
            } catch (Exception e) {
                retryTimes++;
                long waitTime = retryInterval * retryTimes;//使得重试机制更加灵活
                System.out.println("操作失败请等待  " + waitTime + "  秒，后再尝试");
                if (retryTimes >= maxRetryTimes) {
                    throw new RuntimeException("解除用户失败!");
                } else {
                    try {
                        Thread.sleep(waitTime);
                    } catch (InterruptedException interruptedException) {
                        throw new RuntimeException(interruptedException);
                    }
                }
            }
        }
        if (!success) {
            throw new RuntimeException("解除用户拉黑失败!");
        }

    }

    @Transactional
    public void lockUserAndNotAllow(UserBlock userBlock, Integer userId) {
        long retryInterval = 1000;//时间间隔
        int retryTimes = 0;//当前尝试次数
        int maxRetryTimes = 3;//最大尝试次数
        boolean success = false;
        while (!success && retryTimes < maxRetryTimes) {
            try {
                adminUserMapper.addBlockUser(userBlock);
                userMessageMapper.updateUserNotAllowByUserId(userId);
                success = true;
            } catch (Exception e) {
                retryTimes++;
                long waitTime = retryTimes * retryInterval;//使得重试机制更加灵活
                System.out.println("操作失败请等待  " + waitTime + "  秒，后再尝试");
                if (retryTimes >= maxRetryTimes) {
                    throw new RuntimeException("拉黑用户失败!");
                } else {
                    try {
                        Thread.sleep(waitTime);
                    } catch (InterruptedException interruptedException) {
                        throw new RuntimeException(interruptedException);
                    }
                }
            }

        }
        if (!success) {
            throw new RuntimeException("拉黑用户失败!");
        }
    }

    @Override
    public List<UserCountDto> getUserCountByYearAndMonth(Integer year, Integer month) {
        return adminUserMapper.getUserCountByYearAndMonth(year, month);
    }


}
